package com.imooc.user.controller;

import com.imooc.api.BaseController;
import com.imooc.api.controller.user.MyFansControllerApi;
import com.imooc.enums.Sex;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.pojo.vo.FansCountsVO;
import com.imooc.user.service.MyFanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyFansController extends BaseController implements MyFansControllerApi {
    
    @Autowired
    private MyFanService myFanService;

    /**
     * 查询当前用户是否关注作家
     */
    @Override
    public GraceJSONResult isMeFollowThisWriter(String writerId, String fanId) {
        boolean res = this.myFanService.isMeFollowThisWriter(writerId, fanId);
        return GraceJSONResult.ok(res);
    }

    /**
     * 用户关注作家，成为粉丝
     */
    @Override
    public GraceJSONResult follow(String writerId, String fanId) {
        this.myFanService.follow(writerId, fanId);
        return GraceJSONResult.ok();
    }

    /**
     * 取消关注，作家损失粉丝
     */
    @Override
    public GraceJSONResult unfollow(String writerId, String fanId) {
        this.myFanService.unfollow(writerId, fanId);
        return GraceJSONResult.ok();
    }

    /**
     * 查询我的所有粉丝列表
     */
    @Override
    public GraceJSONResult queryAll(String writerId, Integer page, Integer pageSize) {
        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }
        return GraceJSONResult.ok(this.myFanService.queryMyFansList(writerId, page, pageSize));
    }

    /**
     * 查询男女粉丝数量
     */
    @Override
    public GraceJSONResult queryRatio(String writerId) {
        int manCounts = this.myFanService.queryFansCounts(writerId, Sex.man);
        int womanCounts = this.myFanService.queryFansCounts(writerId, Sex.woman);
        FansCountsVO fansCountsVO = new FansCountsVO();
        fansCountsVO.setManCounts(manCounts);
        fansCountsVO.setWomanCounts(womanCounts);
        return GraceJSONResult.ok(fansCountsVO);
    }

    /**
     * 根据地域查询粉丝数量
     */
    @Override
    public GraceJSONResult queryRatioByRegion(String writerId) {
        return GraceJSONResult.ok(this.myFanService.queryRegionRatioCounts(writerId));
    }
}
